<?php echo $this->render('/public/header'); ?>
<?php use yii\widgets\LinkPager; ?>
    <style>
        input[type=file] {
            display: none
        }

        input:hover {
            border: 1px solid #50A7FF !important;
        }
    </style>
    <script src="https://cdn.bootcss.com/element-ui/1.4.2/index.js"></script>
    <link href="https://cdn.bootcss.com/element-ui/1.4.2/theme-default/index.css" rel="stylesheet">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12" id='main'>
        <div class='col-lg-1'>
            <el-upload action='/finance/upload_inland_excel' class="upload_demo" :on-success="handlesuccess">
                <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
        </div>
        <div class='col-lg-1'>
            <button onclick="Page.add()" class="btn btn-success">添加记录</button>
        </div>
        <div class="col-lg-12" style="margin-top:20px">
            <form action="" method="get">
                <div class="form-inline">
                    <div class="form-group">
                        <select class="form-control" name="saleman">
                            <option value=0>--搜索业务员--</option>
                            <?php foreach ($this->params['staff_list'] as $v): ?>
                                <option value=<?php echo $v['job_number']; ?> <?php if ($v['job_number'] == yii::$app->request->get('saleman')) {
                                    echo 'selected';
                                } ?>><?php echo $v['name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <select class="form-control" name="proxy_company">
                            <option value=0>选择代理公司</option>
                            <?php foreach ($this->params['proxy_company_list'] as $v): ?>
                                <option value=<?php echo $v['id']; ?> <?php echo yii::$app->request->get('proxy_company') == $v['id'] ? 'selected' : ""; ?>><?php echo $v['name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="搜索客户" name="customer_name"
                               value="<?php echo yii::$app->request->get('customer_name'); ?>"/>
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="搜索客户编号" name="customer_num"
                               value="<?php echo $_GET['customer_num'] ?? ""; ?>"/>
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="搜索invoice编号" name="invoice"
                               value="<?php echo $_GET['invoice'] ?? ""; ?>"/>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="搜索进仓编号" name="order_num"
                               value="<?php echo $_GET['order_num'] ?? ""; ?>"/>
                    </div>

                    <button class="btn btn-success">搜索</button>
                </div>
            </form>
            <br/>
            <table class="table table-borderd">
                <thead>
                <tr>
                    <th style="width:40px">id</th>
                    <th style="width:100px">日期</th>
                    <th style="width:50px">是否拆分</th>
                    <th>代理公司</th>
                    <th>业务员</th>
                    <th>客户编号</th>
                    <th>客户名称</th>
                    <th>invoice</th>
                    <th>进仓编号</th>
                    <th>数量</th>
                    <th>体积</th>
                    <th>运费</th>
                    <th>进仓费</th>
                    <th>总计</th>
                    <th>备注</th>
                    <th>操作人</th>
                    <th>最后更新时间</th>
                    <th style="width:220px">操作</th>
                </tr>
                </thead>
                <style>.table-input {
                        margin: 0px;
                        padding: 0px;
                        width: 100%;
                        border: none;
                    }</style>
                <?php foreach ($this->params['list'] as $v) : ?>
                    <tr>
                        <td><?php echo $v['id']; ?></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input" class="form-control"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'date',this)"
                                   value="<?php echo $v['date']; ?>"/></td>
                        <td><?php echo $v['pid'] == $v['id'] ? '' : '拆分项' . $v['pid']; ?></td>
                        <td>
                            <select style="border:none;padding:0px;width:100%"
                                    onchange="Page.update(<?php echo $v['id']; ?>,'proxy_company',this)">
                                <option value=0>请选择</option>
                                <?php foreach ($this->params['proxy_company_list'] as $v2): ?>
                                    <option value=<?php echo $v2['id']; ?> <?php echo $v['proxy_company'] == $v2['id'] ? 'selected' : ""; ?>><?php echo $v2['name']; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                        <td>
                            <select style="border:none;padding:0px;width:100%"
                                    onchange="Page.update(<?php echo $v['id']; ?>,'saleman',this)">
                                <option value=0>请选择</option>
                                <?php foreach ($this->params['staff_list'] as $v2): ?>
                                    <option value="<?php echo $v2['job_number']; ?>" <?php if ($v2['job_number'] == $v['saleman']) {
                                        echo 'selected';
                                    } ?>><?php echo $v2['name']; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                        <td><input style="width:100%;border:none;<?php echo $v['same'] ? '' : 'color:red'; ?>"
                                   type="text" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'customer_num',this)"
                                   value="<?php echo $v['customer_num']; ?>"/></td>
                        <td><input style="width:100%;border:none;<?php echo $v['same'] ? '' : 'color:red'; ?>"
                                   type="text" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'customer_name',this)"
                                   value="<?php echo $v['customer_name']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'invoice',this)"
                                   value="<?php echo $v['invoice']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'order_num',this)"
                                   value="<?php echo $v['order_num']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'num',this)"
                                   value="<?php echo $v['num']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'volume',this)"
                                   value="<?php echo $v['volume']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'ship_fee',this)"
                                   value="<?php echo $v['ship_fee']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'in_repo_fee',this)"
                                   value="<?php echo $v['in_repo_fee']; ?>"/></td>
                        <td><input type="text"
                                   style="width:100%;border:none;<?php echo ((int)$v['money_same'] !== 1) ? 'color:red' : ''; ?>"
                                   class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'total_fee',this)"
                                   value="<?php echo $v['total_fee']; ?>"/></td>
                        <td><input type="text" style="width:100%;border:none" class="table-input"
                                   onchange="Page.update(<?php echo $v['id']; ?>,'remark',this)"
                                   value="<?php echo $v['remark']; ?>"/></td>
                        <td>
                            <?php foreach ($this->params['staff_list'] as $v2): ?>
                                <?php if ($v2['job_number'] == $v['last_modify']) {
                                    echo $v2['name'];
                                } ?>
                            <?php endforeach; ?>
                        </td>
                        <td><?php echo $v['last_modify_time'] ? date('Y-m-d H:i:s', $v['last_modify_time']) : ''; ?></td>
                        <td>
                            <button class="btn btn-danger" onclick="del(<?php echo $v['id']; ?>)">删除</button>
                            <button class="btn btn-success"
                                    onclick="lock(<?php echo $v['id']; ?>,<?php echo $v['lock_status']; ?>)">
                                <?php echo $v['lock_status'] == 1 ? '解除锁定' : '锁定'; ?>
                            </button>
                            <?php if ($v['pid'] == $v['id']): ?>
                                <button class="btn btn-primary btn-small" @click="split(<?php echo $v['id']; ?>)">拆分
                                </button>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
            <?php echo LinkPager::widget(['pagination' => $page]); ?>
        </div>
    </div>


    <script>
        new Vue({
            el: "#main",
            data: {},
            methods: {
                handlesuccess: function (res) {
                    if (res.code == 1) {
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        })
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        })
                    }
                },

                split: function (v) {
                    this.$confirm("确认拆分").then(() => {
                        $.post('/table/split-inland', {id: v}, (res) => {
                            if (res.code == 1) {
                                this.$message.success(res.msg);
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1000);
                            } else if (res.code == 0) {
                                this.$message.error(res.msg);
                            }
                        }, 'json')
                    })
                }
            }
        });

        function lock(id, lock_status) {
            let content = lock_status == 1 ? '确认解除锁定' : '确认锁定';
            const data = {};
            data.id = id;
            data.param = 'lock_status';
            data.value = lock_status == 1 ? 2 : 1;

            layer.confirm(content, {}, function () {
                $.post('/table/lock-inland', data, function (res) {
                    if (res.code == 1) {
                        layer.alert(res.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })
        }

        function del(id) {
            layer.alert('确认删除', {}, function () {
                $.post('/table/inland-del', {id: id}, function (res) {
                    if (res.code == 1) {
                        layer.alert(res.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })
        }

        var Page = {};
        Page.update = function (id, column, e) {
            layer.confirm('确认修改', {}, function () {
                var url = "/table/update_inland";
                var data = {};
                data.id = id;
                data.column = column;
                data.value = e.value;
                $.post(url, data, function (res) {
                    if (res.code == 1) {
                        layer.confirm(res.msg, {}, function () {
                            window.location.reload();
                        })
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })
        }
        Page.add = function () {
            zeroModal.show({url: '/api/add_inland'});
        }
    </script>
<?php echo $this->render('/public/footer'); ?>